<?php

namespace addon\hotel\admin\controller;

use app\admin\controller\BaseAdmin;
use app\model\hotel\Baoyu;
use app\model\hotel\Hotel as HotelModel;
use app\model\hotel\HotelApply as HotelApplyModel;
use app\model\shop\ShopApply as ShopApplyModel;
use app\model\system\Address as AddressModel;
use app\model\web\Config as ConfigModel;

class Hotel extends BaseAdmin
{

    public function lists()
    {

        $hotel_model = new HotelModel();
        if (request()->isAjax()) {

            $page = input('page', 1);
            $page_size = input('page_size', PAGE_LIST_ROWS);
            $condition = [
                //['apply_state','=',1]
            ];
            $hotel_name = input('hotel_name', '');
            if ($hotel_name) {
                $condition[] = [ 'hotel_name', 'like', '%' . $hotel_name . '%' ];
            }

            $telphone = input('telphone', '');
            if ($telphone) {
                $condition[] = [ 'telphone', 'like', '%' . $telphone . '%' ];
            }

            $status = input('status', 0);
            if ($status) {
                $condition[] = [ 'status', '=', $status ];
            }

            $is_recommend = input('is_recommend','');
            if ($is_recommend!='') {
                $condition[] = [ 'is_recommend', '=', $is_recommend ];
            }

            $start_time = input('start_time', '');
            $end_time = input('end_time', '');
            if (!empty($start_time) && empty($end_time)) {
                $condition[] = [ "create_time", ">=", strtotime($start_time) ];
            } elseif (empty($start_time) && !empty($end_time)) {
                $condition[] = [ "create_time", "<=", strtotime($end_time) ];
            } elseif (!empty($start_time) && !empty($end_time)) {
                $condition[] = [ "create_time", "between", [ strtotime($start_time), strtotime($end_time) ] ];
            }

            $list = $hotel_model->getHotelPageList($condition, $page, $page_size, 'status desc,sort desc');
            return $list;
        } else {
            return $this->fetch('hotel/lists');
        }

    }


    public function add()
    {
        $hotel_model = new HotelModel();

        if (request()->isAjax()){

            $data = [
                'hotel_name' => input('hotel_name', ''),
                'hotel_img' => input('hotel_img', ''),
                'telphone' => input('telphone', ''),
                'direct' => input('direct', ''),
                //'is_frozen' => input('is_frozen', ''),
                'status' => input('status', ''),
                'province_id' => input('province_id', ''),
                'city_id' => input('city_id', ''),
                'district_id' => input('district_id', ''),
                'address' => input('address', ''),
                'full_address' => input('full_address', ''),
                'longitude' => input('longitude', ''),
                'latitude' => input('latitude', ''),

                'traffic' => input('traffic', ''),
                'hotel_level' => input('hotel_level', 0),
                'business_license' => input('business_license', ''),
                'start_time' => input('start_time', ''),
                'end_time' => input('end_time', ''),
                'commission_rate' => input('commission_rate', ''),

                'images' => input('images', ''),
                'video_url' => input('video_url', ''),
                'introduce' => input('introduce', ''),
                'is_recommend' => input('is_recommend', 0),
                'recommend_reason' => input('recommend_reason', ''),
                'sort' => input('sort', ''),
                'label_format' => input('label_format', ''),
                'equipment_format' => input('equipment_format', ''),

                'hotel_type' => input('hotel_type', 1),
                'business_license_number' => input('business_license_number', ''),
                'business_license_type' => input('business_license_type', ''),
                'business_license_start_time' => input('business_license_start_time', ''),
                'business_license_end_time' => input('business_license_end_time', ''),
                'front_pic_id_card' => input('front_pic_id_card', ''),
                'reverse_pic_id_card' => input('reverse_pic_id_card', ''),
                'id_card_name' => input('id_card_name', ''),
                'id_card_number' => input('id_card_number', ''),
                'id_card_type' => input('id_card_type', ''),
                'id_card_start_time' => input('id_card_start_time', ''),
                'id_card_end_time' => input('id_card_end_time', ''),
                'account_holder' => input('account_holder', ''),
                'bank_number' => input('bank_number', ''),
                'bank_type' => input('bank_type', ''),
                'admin_id' => $this->user_info['uid'],
                'admin_username' => $this->user_info['username'],

                'bank_name' => input('bank_name', ''),
                'bank_img' => input('bank_img', ''),
                'contact_email' => input('contact_email', ''),
                'legal_addr' => input('legal_addr', ''),
                'legal_cert_validity_type' => input('legal_cert_validity_type', 0),
                'busi_type' => input('busi_type', 1),
                'bank_province' => input('bank_province', ''),
                'bank_city' => input('bank_city', ''),
                'bank_district' => input('bank_district', ''),
                'bank_province_name' => input('bank_province_name', ''),
                'bank_city_name' => input('bank_city_name', ''),
                'bank_district_name' => input('bank_district_name', ''),
            ];

            $user_data = [
                'username' => input('username', ''),
                'password' => data_md5(input('password', '')),
            ];

            $res = $hotel_model->addHotel($data, $user_data);
            return $res;

        } else {
            //查询省级数据列表
            $address_model = new AddressModel();
            $list = $address_model->getAreaList([ [ "pid", "=", 0 ], [ "level", "=", 1 ] ]);
            $this->assign("province_list", $list[ "data" ]);

            //获取地址key配置
            $config_model = new ConfigModel();
            $config = $config_model->getMapConfig();
            $this->assign('map_key', $config[ 'data' ][ 'value' ]);

            $label_model = new \app\model\hotel\HotelLabel();
            $label_list = $label_model->getHotelLabelList();
            $this->assign('label_list', $label_list[ 'data' ]);

            $id_card_type_list = $hotel_model->idCardType();

            $this->assign('id_card_type_list', $id_card_type_list);
            return $this->fetch('hotel/add');
        }
    }


    public function edit()
    {
        $hotel_id = input("hotel_id", 0);
        $condition = array (
            [ "hotel_id", "=", $hotel_id ]
        );
        $hotel_model = new HotelModel();
        if (request()->isAjax()) {

            $data = array (
                'hotel_name' => input('hotel_name', ''),
                'hotel_img' => input('hotel_img', ''),
                'telphone' => input('telphone', ''),
                'direct' => input('direct', ''),
                //'is_frozen' => input('is_frozen', ''),
                'status' => input('status', ''),
                'province_id' => input('province_id', ''),
                'city_id' => input('city_id', ''),
                'district_id' => input('district_id', ''),
                'address' => input('address', ''),
                'full_address' => input('full_address', ''),
                'longitude' => input('longitude', ''),
                'latitude' => input('latitude', ''),

                'traffic' => input('traffic', ''),
                'hotel_level' => input('hotel_level', 0),
                'business_license' => input('business_license', ''),
                'start_time' => input('start_time', ''),
                'end_time' => input('end_time', ''),
                'commission_rate' => input('commission_rate', ''),
                'images' => input('images', ''),
                'video_url' => input('video_url', ''),
                'introduce' => input('introduce', ''),
                'is_recommend' => input('is_recommend', 0),
                'recommend_reason' => input('recommend_reason', ''),
                'sort' => input('sort', ''),
                'label_format' => input('label_format', ''),
                'equipment_format' => input('equipment_format', ''),

                'hotel_type' => input('hotel_type', 1),
                'business_license_number' => input('business_license_number', ''),
                'business_license_type' => input('business_license_type', ''),
                'business_license_start_time' => input('business_license_start_time', ''),
                'business_license_end_time' => input('business_license_end_time', ''),
                'front_pic_id_card' => input('front_pic_id_card', ''),
                'reverse_pic_id_card' => input('reverse_pic_id_card', ''),
                'id_card_name' => input('id_card_name', ''),
                'id_card_number' => input('id_card_number', ''),
                'id_card_type' => input('id_card_type', ''),
                'id_card_start_time' => input('id_card_start_time', ''),
                'id_card_end_time' => input('id_card_end_time', ''),
                'account_holder' => input('account_holder', ''),
                'bank_number' => input('bank_number', ''),
                'bank_type' => input('bank_type', ''),

                'bank_name' => input('bank_name', ''),
                'bank_img' => input('bank_img', ''),
                'contact_email' => input('contact_email', ''),
                'legal_addr' => input('legal_addr', ''),
                'legal_cert_validity_type' => input('legal_cert_validity_type', 0),
                'busi_type' => input('busi_type', 1),
                'bank_province' => input('bank_province', ''),
                'bank_city' => input('bank_city', ''),
                'bank_district' => input('bank_district', ''),
                'bank_province_name' => input('bank_province_name', ''),
                'bank_city_name' => input('bank_city_name', ''),
                'bank_district_name' => input('bank_district_name', ''),
            );

            $result = $hotel_model->editHotel($data, $condition);
            return $result;
        } else {
            //查询省级数据列表
            $address_model = new AddressModel();
            $list = $address_model->getAreaList([ [ "pid", "=", 0 ], [ "level", "=", 1 ] ]);
            $this->assign("province_list", $list[ "data" ]);
            $info_result = $hotel_model->getHotelInfo($condition);//门店信息
            $info = $info_result[ "data" ];
            $this->assign("info", $info);
            $this->assign("hotel_id", $hotel_id);

            //获取地址key配置
            $config_model = new ConfigModel();
            $config = $config_model->getMapConfig();
            $this->assign('map_key', $config[ 'data' ][ 'value' ]);

            $label_model = new \app\model\hotel\HotelLabel();
            $label_list = $label_model->getHotelLabelList();
            $this->assign('label_list', $label_list[ 'data' ]);

            $id_card_type_list = $hotel_model->idCardType();
            $this->assign('id_card_type_list', $id_card_type_list);

            return $this->fetch("hotel/edit");
        }
    }

    public function delete()
    {
        if (request()->isAjax()) {
            $hotel_id = input("hotel_id", 0);
            $condition = array (
                [ "hotel_id", "=", $hotel_id ]
            );
            $hotel_model = new HotelModel();
            $result = $hotel_model->deleteHotel($condition);
            return $result;
        }
    }

    /**
     * 冻结分站
     */
    public function frozen()
    {
        $hotel_model = new HotelModel();

        $hotel_id = input('hotel_id', '');
        $is_frozen = input('is_frozen', 0);
        $condition = [
            [ "hotel_id", "=", $hotel_id ]
        ];

        $res = $hotel_model->statusHotel($condition, $is_frozen);
        return $res;
    }


    public function apply()
    {

        $hotel_model = new HotelApplyModel();
        if (request()->isAjax()) {

            $page = input('page', 1);
            $page_size = input('page_size', PAGE_LIST_ROWS);

            $condition = [
                //['apply_state','=',0]
            ];

            $apply_state = input('apply_state', 0);
            if ($apply_state!=''){
                $condition[] = [ 'apply_state', '=', $apply_state ];
            }


            $hotel_name = input('hotel_name', '');
            if ($hotel_name) {
                $condition[] = [ 'hotel_name', 'like', '%' . $hotel_name . '%' ];
            }

            $telphone = input('telphone', '');
            if ($telphone) {
                $condition[] = [ 'telphone', 'like', '%' . $telphone . '%' ];
            }

            $status = input('status', 0);
            if ($status) {
                $condition[] = [ 'status', '=', $status ];
            }

            $is_recommend = input('is_recommend','');
            if ($is_recommend!='') {
                $condition[] = [ 'is_recommend', '=', $is_recommend ];
            }

            $start_time = input('start_time', '');
            $end_time = input('end_time', '');
            if (!empty($start_time) && empty($end_time)) {
                $condition[] = [ "create_time", ">=", strtotime($start_time) ];
            } elseif (empty($start_time) && !empty($end_time)) {
                $condition[] = [ "create_time", "<=", strtotime($end_time) ];
            } elseif (!empty($start_time) && !empty($end_time)) {
                $condition[] = [ "create_time", "between", [ strtotime($start_time), strtotime($end_time) ] ];
            }

            $list = $hotel_model->getApplyPageList($condition, $page, $page_size, 'status desc,sort desc');
            return $list;
        } else {
            return $this->fetch('hotel/apply');
        }

    }

    public function detail()
    {
        $apply_id = input("apply_id", 0);
        $condition = array (
            [ "apply_id", "=", $apply_id ]
        );
        $hotel_model = new HotelModel();
        $shop_apply_model = new ShopApplyModel();

        $hotel_apply_model = new HotelApplyModel();
        $info_result = $hotel_apply_model->getApplyInfo($condition);//门店信息
        $info = $info_result[ "data" ];

        $val = $shop_apply_model->transformSeckillTime($info);
        $info[ 'start_time_show' ] = "{$val['start_hour']}:{$val['start_minute']}";
        $info[ 'end_time_show' ] = "{$val['end_hour']}:{$val['end_minute']}";
        $info[ 'images' ] = explode(',',$info[ 'images' ]);
        $this->assign("apply_detail", $info);
        $id_card_type_list = $hotel_model->idCardType();

        $this->assign('id_card_type_list', $id_card_type_list);
        return $this->fetch("hotel/detail");
    }

    //审核
    public function applyAudit()
    {
        if (request()->isAjax()){
            $apply_id = input("apply_id", 0);
            $apply_state = input("apply_state", 0);
            $commission_rate = input('commission_rate',0);
            $direct = input('direct',0);
            $reason = input('reason','');

            $user_info = [
                'username' => input('username', ''),
                'password' => data_md5(input('password', '')),
            ];
            $hotel_model = new HotelApplyModel();

            $data = [
                'apply_state'=>$apply_state,
                'audit_time' => time(),
                'reason'=>$reason,
                //'direct'=>$direct,
                //'commission_rate'=>$commission_rate
            ];

            return $hotel_model->applyPass($apply_id,$data,$user_info,$commission_rate,$direct);
        }
    }

    public function submitHuifu()
    {
        $hotel_model = new HotelModel();

        $hotel_id = input('hotel_id', '');

        $condition = [
            [ "hotel_id", "=", $hotel_id ]
        ];

        $res = $hotel_model->submitHuifu($condition);
        return $res;
    }
}